﻿using MediatR;
using S.Produce.Api.Application.Commands.BreedingCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.BreedingCommandHandlers
{
    public class BreedingDeleteCommandHandler : IRequestHandler<BreedingDeleteCommand, int>
    {
        private readonly IRepository<Breeding> repository;

        public BreedingDeleteCommandHandler(IRepository<Breeding> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(BreedingDeleteCommand request, CancellationToken cancellationToken)
        {
            var ids = request.Ids.Split(",");
            var count = 0;
            foreach (var item in ids)
            {
                var info = await repository.GetAsync(int.Parse(item));
                info.IsDel = true;
                count += await repository.UpdateAsync(info);
            }
            return count;
        }
    }
}
